package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import observer.Observer;
import observer.Subject;

import model.Afficheur;
import model.EmetteurSonore;
import model.Molette;
import model.MoteurMetronomeImpl;

import command.CommandDec;
import command.CommandInc;
import command.CommandStart;
import command.CommandStop;

import controller.ControllerImpl;


public class IHM extends JPanel implements Afficheur, Molette, Subject, EmetteurSonore{

	private JButton buttonStart;
	private JButton buttonStop;
	private JButton buttonInc;
	private JButton buttonDec;

	private JSlider sliderTempo;

	private JTextField afficheur;

	private JLabel led1;
	private JLabel led2;

	private GridBagConstraints gbc;

	private static CommandStart cmdStart;
	private static CommandStop cmdStop;
	private static CommandInc cmdInc;
	private static CommandDec cmdDec;

	private static ControllerImpl controller;
	
	private Observer observer;

	public IHM(){

		observer = null;

		// Creation des commandes
		cmdStart = new CommandStart(controller,this);
		cmdStop = new CommandStop(controller, this);
		cmdInc = new CommandInc(controller, this);
		cmdDec = new CommandDec(controller, this);

		gbc = new GridBagConstraints();

		// Initialisation du container
		this.setLayout(new GridBagLayout());

		// Creation des composants
		buttonStart = new JButton("START");	
		buttonStart.addActionListener(new StartAction());

		buttonStop = new JButton("STOP");	
		buttonStop.setPreferredSize(buttonStart.getPreferredSize());
		buttonStop.setMinimumSize(buttonStart.getPreferredSize());
		buttonStop.addActionListener(new StopAction());

		buttonInc = new JButton("INC");	
		buttonInc.setPreferredSize(buttonStart.getPreferredSize());
		buttonInc.setMinimumSize(buttonStart.getPreferredSize());
		buttonInc.addActionListener(new IncAction());

		buttonDec = new JButton("DEC");
		buttonDec.setPreferredSize(buttonStart.getPreferredSize());
		buttonDec.setMinimumSize(buttonStart.getPreferredSize());
		buttonDec.addActionListener(new DecAction());

		sliderTempo = new JSlider(0,100);
		sliderTempo.setPreferredSize(new Dimension(80, 20));
		sliderTempo.setMinimumSize(new Dimension(80, 20));
		sliderTempo.setBackground(Color.WHITE);

		afficheur = new JTextField();
		afficheur.setPreferredSize(new Dimension(110, 20));
		afficheur.setMinimumSize(new Dimension(110, 20));
		afficheur.setBackground(new Color(202,227,249));

		led1 = new JLabel("LED 1", SwingConstants.RIGHT);
		led2 = new JLabel("LED 2", SwingConstants.RIGHT);

		// Definition du placement du slider puis ajout au container
		gbc.gridx = gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 2;
		gbc.insets = new Insets(5, 10, 0, 0);
		this.add(sliderTempo, gbc);

		// Definition du placement du textfield puis ajout au container
		gbc.gridx = 1 ;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.gridheight = 2;
		gbc.insets = new Insets(5, 10, 0, 0);
		this.add(afficheur, gbc);

		// Definition du placement de la led 1 puis ajout au container
		gbc.gridx = 3 ;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(5, 10, 0, 10);
		this.add(led1, gbc);

		// Definition du placement de la led 2 puis ajout au container
		gbc.gridx = 3 ;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(5, 10, 10, 10);
		this.add(led2, gbc);

		// Definition du placement du boutton start puis ajout au container
		gbc.gridx = 0 ;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(5, 10, 0, 0);
		this.add(buttonStart, gbc);

		// Definition du placement du boutton stop puis ajout au container
		gbc.gridx = 1 ;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(5, 10, 0, 0);
		this.add(buttonStop, gbc);

		// Definition du placement du boutton inc puis ajout au container
		gbc.gridx = 2 ;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(5, 10, 0, 0);
		this.add(buttonInc, gbc);

		// Definition du placement du boutton dec puis ajout au container
		gbc.gridx = 3 ;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(5, 10, 0, 0);
		this.add(buttonDec, gbc);

		this.setBackground(Color.WHITE);		
		this.setVisible(true);
	}

//-------------Classe interne pour gerer les actions sur les differents bouttons de l'interface-------------//	
	
	class StartAction implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			cmdStart.exectue();			
		}

	}

	class StopAction implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			cmdStop.exectue();			
		}

	}

	class IncAction implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			cmdInc.exectue();			
		}

	}

	class DecAction implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			cmdDec.exectue();			
		}

	}
	
//-----------------------------------------------------------------------------------------------------------//		

	public void afficherTempo(int valeurTempo) {
		afficheur.setText("" + valeurTempo);
		notifyObserver(this);
	}


	public void allumerLED(int numLED) {
		if(numLED == 1){
			led1.setIcon(new ImageIcon("./src/images/bouttonOrange.jpg"));
		}else if(numLED == 2){
			led2.setIcon(new ImageIcon("./src/images/bouttonVert.jpg"));
		}
		notifyObserver(this);
	}


	public void eteindreLED(int numLED) {	
		if(numLED == 1){
			led1.setIcon(new ImageIcon(""));
		}else if(numLED == 2){
			led2.setIcon(new ImageIcon(""));
		}
		notifyObserver(this);
	}


	public float position() {
		System.out.println(" valeur : " + sliderTempo.getValue());
		return sliderTempo.getValue();
		
	}
	
	public void emettreClic() {
		
	}
	
	public void attach(Observer o) {
		this.observer = (Observer) o;
	}

	public void detach(Observer o) {
		if(observer==o){
			observer=null;
		}
	}

	public void notifyObserver(Subject s){
		observer.updateByMoteurMetronome(this);
	}

//------------------------------- Main de l'application --------------------------------------------------//	
	
	public static void main( String[] args){

		JFrame f = new JFrame("Metronome");	
		MoteurMetronomeImpl mmi = new MoteurMetronomeImpl();
		IHM ihm = new IHM();
		ControllerImpl ci = new ControllerImpl(mmi,ihm);
		
		mmi.attach(ci);
		ihm.attach(ci);

		f.add(ihm);
		f.setSize(400,150);
		f.setResizable(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);

	}

}
